package com.lanvideo.service;

import com.lanvideo.dao.PlaylistDao;
import com.lanvideo.dao.PlaylistVideoDao;
import com.lanvideo.entity.Playlist;
import com.lanvideo.entity.PlaylistVideo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 播放列表服务
 */
@Service
public class PlaylistService {

    @Autowired
    private PlaylistDao playlistDao;

    @Autowired
    private PlaylistVideoDao playlistVideoDao;

    /**
     * 创建播放列表
     */
    public boolean createPlaylist(Playlist playlist) {
        int result = playlistDao.createPlaylist(playlist);
        return result > 0;
    }

    /**
     * 更新播放列表
     */
    public boolean updatePlaylist(Playlist playlist) {
        int result = playlistDao.updatePlaylist(playlist);
        return result > 0;
    }

    /**
     * 删除播放列表
     */
    public boolean deletePlaylist(Integer id) {
        int result = playlistDao.deletePlaylist(id);
        return result > 0;
    }

    /**
     * 根据ID获取播放列表
     */
    public Playlist getPlaylistById(Integer id) {
        return playlistDao.getPlaylistById(id);
    }

    /**
     * 获取用户的播放列表
     */
    public List<Playlist> getUserPlaylists(Integer userId) {
        return playlistDao.getUserPlaylists(userId);
    }

    /**
     * 获取公开的播放列表
     */
    public List<Playlist> getPublicPlaylists() {
        return playlistDao.getPublicPlaylists();
    }

    /**
     * 搜索播放列表
     */
    public List<Playlist> searchPlaylists(String keyword) {
        return playlistDao.searchPlaylists(keyword);
    }

    /**
     * 检查播放列表是否属于用户
     */
    public boolean isPlaylistOwner(Integer playlistId, Integer userId) {
        return playlistDao.isPlaylistOwner(playlistId, userId);
    }

    /**
     * 获取播放列表数量
     */
    public int getUserPlaylistCount(Integer userId) {
        return playlistDao.getUserPlaylistCount(userId);
    }

    /**
     * 添加视频到播放列表
     */
    public boolean addVideoToPlaylist(Integer playlistId, Integer videoId) {
        try {
            int result = playlistVideoDao.addVideoToPlaylist(playlistId, videoId);
            return result > 0;
        } catch (Exception e) {
            // 如果视频已经在播放列表中，返回false
            return false;
        }
    }

    /**
     * 从播放列表移除视频
     */
    public boolean removeVideoFromPlaylist(Integer playlistId, Integer videoId) {
        int result = playlistVideoDao.removeVideoFromPlaylist(playlistId, videoId);
        return result > 0;
    }

    /**
     * 更新视频在播放列表中的顺序
     */
    public boolean updateVideoOrder(Integer playlistId, Integer videoId, Integer orderNum) {
        int result = playlistVideoDao.updateVideoOrder(playlistId, videoId, orderNum);
        return result > 0;
    }

    /**
     * 获取播放列表中的所有视频
     */
    public List<PlaylistVideo> getPlaylistVideos(Integer playlistId) {
        return playlistVideoDao.getPlaylistVideos(playlistId);
    }

    /**
     * 检查视频是否在播放列表中
     */
    public boolean isVideoInPlaylist(Integer playlistId, Integer videoId) {
        return playlistVideoDao.isVideoInPlaylist(playlistId, videoId);
    }

    /**
     * 获取视频在播放列表中的信息
     */
    public PlaylistVideo getPlaylistVideo(Integer playlistId, Integer videoId) {
        return playlistVideoDao.getPlaylistVideo(playlistId, videoId);
    }

    /**
     * 获取播放列表中的视频数量
     */
    public int getPlaylistVideoCount(Integer playlistId) {
        return playlistVideoDao.getPlaylistVideoCount(playlistId);
    }

    /**
     * 清空播放列表中的所有视频
     */
    public boolean clearPlaylist(Integer playlistId) {
        int result = playlistVideoDao.clearPlaylist(playlistId);
        return result > 0;
    }

    /**
     * 获取用户的所有播放列表视频
     */
    public List<PlaylistVideo> getUserPlaylistVideos(Integer userId) {
        return playlistVideoDao.getUserPlaylistVideos(userId);
    }
}
